import pygame
import gruop.SUBSAVE


def transform(blit_p, cut, X, Y):
    """an method to change every part of restore pic to make it restore smoothly
        NOW IS STILL DEVELOPING ,NOT THE LESTER"""
    wide = cut.get_width()
    high = cut.get_height()

    center = (X / 2, Y / 2)
    center_cut = (blit_p[0] + wide / 2, blit_p[1] + high / 2)

    if center_cut[0] < center[0]:
        blit_p = (blit_p[0] - 1, blit_p[1])
    if center_cut[0] > center[0]:
        blit_p = (blit_p[0] + 2, blit_p[1])
    else:
        pass

    return cut, blit_p


def restore_tool(ship_name, ):
    """拼图用的函数"""

    # 用于存储相应参数
    blit_place = [None]
    cut_place = [None]
    restore_way = []
    printer = []

    # 文件信息读取，分类
    with open("Mesh\\" + ship_name + "-mesh.obj", 'r')as info:
        for msg in info.readlines():

            if msg[0] == "g":
                continue

            elif msg[0] == "v" and msg[1] != 't':
                msg = msg[:-3]
                msg = msg.split(" ")
                msg = msg[1:]
                msg = [int(msg[0]), int(msg[1])]
                blit_place.append(msg)

            elif msg[0] == "v" and msg[1] == "t":
                msg = msg[:-1]
                msg = msg.split(" ")
                msg = msg[1:]
                msg = [float(msg[0]), float(msg[1])]
                cut_place.append(msg)

            elif msg[0] == 'f':
                msg = msg[:-1]
                msg = msg.split(" ")
                msg = [int(msg[1].split('/')[0]),
                       int(msg[2].split('/')[0]),
                       int(msg[3].split('/')[0]),
                       ]
                restore_way.append(msg)

    # 拼图准备
    temp = ([], [])
    for num in blit_place[1:]:
        temp[0].append(num[0])
        temp[1].append(num[1])

    X = (max(temp[0]) - min(temp[0]))
    Y = (max(temp[1]) - min(temp[1]))

    del temp

    # 背景准备

    bg = pygame.Surface((X, Y), flags=pygame.SRCALPHA, depth=32)

    # 图片加载
    img = pygame.image.load("Texture2D\\" + ship_name + '.png')

    # img =pygame.transform.flip(img,False,False)

    width = img.get_width()
    height = img.get_height()

    # 坐标镜像处理

    for num in range(len(blit_place) - 1):
        blit_place[num + 1][0] = -blit_place[num + 1][0]
        blit_place[num + 1][1] = Y - blit_place[num + 1][1]
        cut_place[num + 1][0] = cut_place[num + 1][0]
        cut_place[num + 1][1] = 1 - cut_place[num + 1][1]
    # 切割模块
    for index in restore_way:
        # 索引，拆分
        blit_p = [blit_place[index[0]], blit_place[index[1]], blit_place[index[2]]]
        cut_p = [cut_place[index[0]], cut_place[index[1]], cut_place[index[2]]]

        blit_area = [min(blit_p[0][0], blit_p[1][0], blit_p[2][0]), min(blit_p[0][1], blit_p[1][1], blit_p[2][1])]

        cut_x = int(min(cut_p[0][0], cut_p[1][0], cut_p[2][0]) * width)
        cut_y = int(min((cut_p[0][1], cut_p[1][1], cut_p[2][1])) * height)

        wide = int(
            (max(cut_p[0][0], cut_p[1][0], cut_p[2][0]) - min(cut_p[0][0], cut_p[1][0], cut_p[2][0])) * width) + 1
        high = int(
            (max(cut_p[0][1], cut_p[1][1], cut_p[2][1]) - min(cut_p[0][1], cut_p[1][1], cut_p[2][1])) * height) + 1

        cut_size = pygame.Rect(cut_x, cut_y, wide, high)

        cut = img.subsurface(cut_size)

        # cut, blit_area = transform(blit_area, cut, X, Y)

        # cut =pygame.transform.flip(cut,False,True)

        printer.append([blit_area, cut])



    # 开始拼图
    gruop.SUBSAVE.sub_save(printer, ship_name, bg)

    for index in printer:
        bg.blit(index[1], index[0])

    pic = pygame.transform.flip(bg, False, False)

    pygame.image.save(pic, "out\\" + ship_name + ".png")


def file_explorer():
    """a method to read the pic which had been restored,and tell the user"""
    pass
